package com.dd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.entity.DdActivity;
import com.dd.entity.DdActivityUser;
import com.dd.entity.DdShop;
import com.dd.entity.DdShopgoods;
import com.dd.entity.DdSystemuser;
import com.dd.entity.DdUser;
import com.dd.entity.DdUserpage;
import com.dd.entity.DdUserpay;
import com.dd.util.DateStrFormat;

public class UserAction extends BaseAction {

	public ActionForward refund(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			Integer logid = (Integer) session.getAttribute("logid");
			if (logid == null) {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}
			DdUser du = this.getDdUserDAO().findById(logid);
			if (du.getAmount().doubleValue() == 0F) {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>2</result></root>");
				return null;
			}

			DdUserpay up = new DdUserpay();
			up.setAmount(du.getAmount());
			up.setPaytype(3);
			up.setStatus(1);
			up.setUserid(du.getId());
			up.setBackaccount(request.getParameter("backaccount"));
			up.setRemark(request.getParameter("reason"));

			du.setAmount(0F);
			du.setAmountfree(0F);
			this.getDdUserDAO().saveOrUpdate(du);
			this.getDdUserpayDAO().saveOrUpdate(up);
			sbf.append("<result>1</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward refundconfirm(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			Integer logid = (Integer) session.getAttribute("logid");
			if (logid == null) {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}

			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward userlist(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			List li = this.getDdUserDAO().findAll();
			sbf.append("<users>");
			for (int i = 0; i < li.size(); i++) {
				DdUser du = (DdUser) li.get(i);
				sbf.append("<user id=\"" + du.getId() + "\" logid=\""
						+ du.getLogid() + "\" />");
			}
			sbf.append("</users></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward checkname(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			String logid = request.getParameter("logid");
			DdUser du = this.getDdUserDAO().findBylogid(logid);
			if (du != null) {
				sbf.append("<result>1</result></root>");
				out.print(sbf.toString());
				return null;
			}
			sbf.append("<result>0</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	// 验证手机号码
	// 发送短信,随机生成5位数字
	public ActionForward checkphone(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		if (logid == null) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			DdUser du = this.getDdUserDAO().findById(logid);
			String phone = request.getParameter("phone");

			Random r = new Random();
			int code = r.nextInt(80000) + 10000;
			du.setCode(code + "");
			this.getDdUserDAO().saveOrUpdate(du);
			// todo 发送短信代码

			sbf.append("<result>1</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	// 用户手机收到短信后验证
	public ActionForward confirmphone(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			String code = request.getParameter("code");
			if (session.getAttribute("code") == null) {
				sbf.append("<result>3</result></root>");
				out.print(sbf.toString());
				return null;
			}
			int sessioncode = (Integer) session.getAttribute("code");
			if (sessioncode != Integer.parseInt(code)) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			String phone = (String) session.getAttribute("logid");
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			if (du == null) {
				sbf.append("<error>1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			du.setStatus(1);
			this.getDdUserDAO().saveOrUpdate(du);
			sbf.append("<result>1</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward newuser(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			String logid = request.getParameter("logid");
			String password = request.getParameter("password");

			String phone = request.getParameter("phone");
			if (logid == null || logid.trim().equals("") || password == null
					|| password.trim().equals("") || phone == null
					|| phone.trim().equals("")) {
				sbf.append("<error>2</error></root>");
				out.print(sbf.toString());
				return null;
			}
			DdUser du = this.getDdUserDAO().findBylogid(logid);
			if (du != null) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			du = this.getDdUserDAO().findByPhone(phone);
			if (du != null) {
				sbf.append("<result>3</result></root>");
				out.print(sbf.toString());
				return null;
			}
			du = new DdUser();
			du.setLogid(logid);
			du.setPassword(password);
			du.setStatus(1);
			du.setNickname("");
			du.setCredit(1);
			du.setPhone(phone);
			du.setRegdate(new Date());
			this.getDdUserDAO().saveOrUpdate(du);
			// int code = new Random().nextInt(500000)+100000;
			// System.out.println(code);
			session.setAttribute("logid", du.getId());
			sbf.append("<result>1</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward edit(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("logid") == null) {
				sbf.append("<error>-1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			Integer logid = (Integer) session.getAttribute("logid");
			DdUser du = this.getDdUserDAO().findById(logid);
			this.getUserService().editUser(request, du, sbf);
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward login(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			DdUser du = this.getDdUserDAO().findBylogid(
					request.getParameter("logid"));
			if (du == null) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			if (!du.getPassword().equals(request.getParameter("password"))) {
				sbf.append("<result>3</result></root>");
				out.print(sbf.toString());
				return null;
			}
			du.setLastlogin(new Date());
			this.getDdUserDAO().saveOrUpdate(du);
			String name = du.getName() == null ? "1" : du.getName();
			String phone = du.getPhone() == null ? "1" : du.getPhone();
			int sign = 0;// 未签到

			if (du.getLastcheck() == null) {
				sign = 0;

			} else {
				java.util.Calendar calst = java.util.Calendar.getInstance();
				java.util.Calendar caled = java.util.Calendar.getInstance();
				calst.setTime(du.getLastcheck());
				caled.setTime(new Date());
				calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
				calst.set(java.util.Calendar.MINUTE, 0);
				calst.set(java.util.Calendar.SECOND, 0);
				caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
				caled.set(java.util.Calendar.MINUTE, 0);
				caled.set(java.util.Calendar.SECOND, 0);
				// 得到两个日期相差的天数
				int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
						.getTime().getTime() / 1000)) / 3600 / 24;
				// System.out.println("days:"+days);
				if (days <= 0) {// 最后签到日期就是今天了,不能再签
					sign = 1;
				}
			}

			sbf.append("<result>1</result><name>" + name + "</name><sex>"
					+ du.getSex() + "</sex><phone>" + phone + "</phone><sign>"
					+ sign + "</sign></root>");
			// System.out.println(sbf.toString());
			session.setAttribute("logid", du.getId());
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	// 签到
	public ActionForward signin(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("logid") == null) {
				sbf.append("<error>-1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			Integer logid = (Integer) session.getAttribute("logid");
			DdUser du = this.getDdUserDAO().findById(logid);
			int score = 5;
			if (du.getLastcheck() == null) {
				du.setLastcheck(new Date());
				du.setCheckdays(1);

			} else {
				java.util.Calendar calst = java.util.Calendar.getInstance();
				java.util.Calendar caled = java.util.Calendar.getInstance();
				calst.setTime(du.getLastcheck());
				caled.setTime(new Date());
				calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
				calst.set(java.util.Calendar.MINUTE, 0);
				calst.set(java.util.Calendar.SECOND, 0);
				caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
				caled.set(java.util.Calendar.MINUTE, 0);
				caled.set(java.util.Calendar.SECOND, 0);
				// 得到两个日期相差的天数
				int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
						.getTime().getTime() / 1000)) / 3600 / 24;
				if (days == 1) {// 连续签到
					du.setLastcheck(new Date());
					du.setCheckdays(du.getCheckdays() + 1);
					score = 5 * du.getCheckdays();
				} else if (days > 1) {// 中断
					du.setLastcheck(new Date());
					du.setCheckdays(1);

				} else { // <=0-已经签到过了
					sbf.append("<result>2</result></root>");
					out.print(sbf.toString());
					return null;
				}
			}
			if (score > 30) {
				score = 30;
			}
			du.setScore(du.getScore() + score);
			this.getDdUserDAO().saveOrUpdate(du);
			sbf.append("<result>" + score + "</result><days>"
					+ du.getCheckdays() + "</days></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	// 签到
	public ActionForward join(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();

		PrintWriter out = response.getWriter();
		try {
			// 不用登陆
			int aid = Integer.parseInt(request.getParameter("aid"));
			DdActivity da = this.getDdActivityDAO().findDdActivityById(aid);
			long curtimestamp = System.currentTimeMillis();
			long begtimestamp = da.getBegdate().getTime();
			long endtimestamp = da.getEnddate().getTime();
			// 过期
			if (curtimestamp < begtimestamp || curtimestamp > endtimestamp) {
				out.print("{\"result\":2}");
				out.flush();
				out.close();
				return null;
			}
			String phone = request.getParameter("phone");
			DdActivityUser dau = this.getDdActivityDAO().findByAidAndPhone(aid,
					phone);
			if (dau != null) {
				out.print("{\"result\":3}");
				out.flush();
				out.close();
				return null;
			}
			dau = new DdActivityUser();
			dau.setActivitydate(new Date());
			dau.setAid(aid);
			dau.setPhone(request.getParameter("phone"));
			dau.setUsername(request.getParameter("username"));
			dau.setWbname(request.getParameter("wbname"));
			dau.setAge(request.getParameter("age"));
			dau.setSex(Integer.parseInt(request.getParameter("sex")));
			dau.setStatus(0);
			dau.setUid(0);
			dau.setRemark(request.getParameter("remark"));
			this.getDdActivityDAO().saveOrUpdateDAU(dau);
			da.setCurcount(da.getCurcount().intValue() + 1);
			this.getDdActivityDAO().saveOrUpdateDA(da);
			out.print("{\"result\":1}");
			out.flush();
			out.close();
			return null;
			/*
			 * if (session.getAttribute("logid") == null) { int aid =
			 * Integer.parseInt(request.getParameter("aid")); DdActivity da =
			 * this.getDdActivityDAO().findDdActivityById(aid); long
			 * curtimestamp = System.currentTimeMillis(); long begtimestamp =
			 * da.getBegdate().getTime(); long endtimestamp =
			 * da.getEnddate().getTime(); //过期
			 * if(curtimestamp<begtimestamp||curtimestamp>endtimestamp){
			 * out.print("{\"result\":2}"); out.flush(); out.close(); return
			 * null; } DdActivityUser dau = new DdActivityUser();
			 * dau.setActivitydate(new Date()); dau.setAid(aid);
			 * dau.setPhone(request.getParameter("phone"));
			 * dau.setUsername(request.getParameter("username"));
			 * dau.setSex(Integer.parseInt(request.getParameter("sex")));
			 * dau.setStatus(0); dau.setUid(0);
			 * dau.setRemark(request.getParameter("remark"));
			 * this.getDdActivityDAO().saveOrUpdateDAU(dau);
			 * da.setCurcount(da.getCurcount().intValue()+1);
			 * this.getDdActivityDAO().saveOrUpdateDA(da);
			 * out.print("{\"result\":1}"); out.flush(); out.close(); return
			 * null; }else{ Integer logid = (Integer)
			 * session.getAttribute("logid"); int aid =
			 * Integer.parseInt(request.getParameter("aid")); DdUser du =
			 * this.getDdUserDAO().findById(logid); DdActivity da =
			 * this.getDdActivityDAO().findDdActivityById(aid); //参数错
			 * if(du==null||da==null){ out.print("{\"result\":-1}");
			 * out.flush(); out.close(); return null; } long curtimestamp =
			 * System.currentTimeMillis(); long begtimestamp =
			 * da.getBegdate().getTime(); long endtimestamp =
			 * da.getEnddate().getTime(); //过期
			 * if(curtimestamp<begtimestamp||curtimestamp>endtimestamp){
			 * out.print("{\"result\":2}"); out.flush(); out.close(); return
			 * null; } DdActivityUser dau =
			 * this.getDdActivityDAO().findByAidAndUid(aid, logid); //已经报过名
			 * if(dau!=null){ out.print("{\"result\":3}"); out.flush();
			 * out.close(); return null; } dau = new DdActivityUser();
			 * dau.setActivitydate(new Date()); dau.setAid(aid);
			 * dau.setPhone(request.getParameter("phone"));
			 * dau.setUsername(request.getParameter("username"));
			 * dau.setSex(Integer.parseInt(request.getParameter("sex")));
			 * dau.setStatus(0); dau.setUid(logid);
			 * dau.setRemark(request.getParameter("remark"));
			 * this.getDdActivityDAO().saveOrUpdateDAU(dau);
			 * da.setCurcount(da.getCurcount().intValue()+1);
			 * this.getDdActivityDAO().saveOrUpdateDA(da);
			 * out.print("{\"result\":1}"); out.flush(); out.close(); return
			 * null; }
			 */
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward loginout(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {

			sbf.append("<result>1</result></root>");
			// session.setAttribute("logid", null);
			session.removeAttribute("logid");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward userinfo(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			String ulogid = request.getParameter("logid");
			if (ulogid == null) {
				if (session.getAttribute("logid") == null) {
					sbf.append("<error>-1</error></root>");
					out.print(sbf.toString());
					return null;
				}
				Integer logid = (Integer) session.getAttribute("logid");

				DdUser du = this.getDdUserDAO().findById(logid);
				this.getUserService().userInfo(request, du, sbf);
				sbf.append("</root>");
				out.print(sbf.toString());
			} else {

				DdUser du = this.getDdUserDAO().findBylogid(ulogid);
				this.getUserService().userInfo(request, du, sbf);
				sbf.append("</root>");
				out.print(sbf.toString());

			}

			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward getMoney(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		String did = request.getParameter("did");
		// 1:100积分换1蛋币
		// 11:1000换11
		// 58:5000换58
		// 115:10000换120
		try {
			if (logid == null) {// 网站用户尚未登录
				// 返回-1
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}
			DdUser du = this.getDdUserDAO().findById(logid);
			if (did.equals("1")) {
				if (du.getScore().intValue() < 100) {
					out
							.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>2</error></root>");
					return null;
				}
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
				du.setScore(du.getScore() - 100);
				du.setAmountfree(du.getAmountfree() + 1);
				this.getDdUserDAO().saveOrUpdate(du);
			} else if (did.equals("11")) {
				if (du.getScore().intValue() < 1000) {
					out
							.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>2</error></root>");
					return null;
				}
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
				du.setScore(du.getScore() - 100);
				du.setAmountfree(du.getAmountfree() + 11);
				this.getDdUserDAO().saveOrUpdate(du);
			} else if (did.equals("58")) {
				if (du.getScore().intValue() < 5000) {
					out
							.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>2</error></root>");
					return null;
				}
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
				du.setScore(du.getScore() - 5000);
				du.setAmountfree(du.getAmountfree() + 58);
				this.getDdUserDAO().saveOrUpdate(du);
			} else if (did.equals("125")) {
				if (du.getScore().intValue() < 10000) {
					out
							.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>2</error></root>");
					return null;
				}
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
				du.setScore(du.getScore() - 10000);
				du.setAmountfree(du.getAmountfree() + 125);
				this.getDdUserDAO().saveOrUpdate(du);
			} else {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>2</error></root>");
				return null;
			}

			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward paylist(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("logid") == null) {
				sbf.append("<error>-1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			Integer logid = (Integer) session.getAttribute("logid");
			List list = this.getDdUserpayDAO().findByUserid(logid);
			for (int i = 0; i < list.size(); i++) {
				DdUserpay dup = (DdUserpay) list.get(i);
				if (dup.getStatus().intValue() == 1
						&& dup.getPaytype().intValue() == 1) {
					continue;
				}
				float ft = dup.getAmount() + dup.getAmountfree();
				int scale = 2;// 设置位数
				int roundingMode = 4;// 表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
				BigDecimal bd = new BigDecimal((double) ft);
				bd = bd.setScale(scale, roundingMode);
				ft = bd.floatValue();
				sbf.append("<pay id=\"" + dup.getId() + "\" payid=\""
						+ (dup.getId() + 200000000) + "\" paytype=\""
						+ dup.getPaytype() + "\" paydate=\""
						+ DateStrFormat.getCurrentTime(dup.getPaydate())
						+ "\" amount=\"" + ft + "\" paystatus=\""
						+ dup.getStatus() + "\" remark=\"" + dup.getRemark()
						+ "\" />");
			}
			sbf.append("</root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward addpage(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("logid") == null) {
				sbf.append("<error>-1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			String shopid = request.getParameter("shopid");
			if (shopid == null) {
				sbf.append("<error>-2</error></root>");
				out.print(sbf.toString());
				return null;
			}
			DdShop ds = this.getDdShopDAO().findById(Integer.parseInt(shopid));
			if (ds == null || ds.getAlive().intValue() == 0) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			if (this.getDdUserpageDAO().isExist(
					(Integer) session.getAttribute("logid"),
					Integer.parseInt(shopid))) {
				sbf.append("<result>3</result></root>");
				out.print(sbf.toString());
				return null;
			}
			DdUserpage dus = new DdUserpage();
			dus.setUserid((Integer) session.getAttribute("logid"));
			dus.setShopid(Integer.parseInt(shopid));
			this.getDdUserpageDAO().saveOrUpdate(dus);
			sbf.append("<result>1</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward removepage(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("logid") == null) {
				sbf.append("<error>1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			int shopid = Integer.parseInt(request.getParameter("id"));
			DdUserpage dus = this.getDdUserpageDAO().findByUserAndShopid(
					(Integer) session.getAttribute("logid"), shopid);
			if (dus == null) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			this.getDdUserpageDAO().delete(dus);
			sbf.append("<result>1</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward userpage(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("logid") == null) {
				sbf.append("<error>-1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			int logid = (Integer) session.getAttribute("logid");
			List list = this.getDdUserpageDAO().findByUserid(logid, 0, 100);

			for (int i = 0; i < list.size(); i++) {
				DdUserpage dsu = (DdUserpage) list.get(i);
				DdShop ds = this.getDdShopDAO().findById(dsu.getShopid());
				sbf
						.append("<shop  id=\"" + ds.getId() + "\" name=\""
								+ ds.getName() + "\" type=\"" + ds.getType()
								+ "\"   >");

				sbf.append("</shop>");
			}
			sbf.append("</root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 商家登录
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward shoplogin(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			DdSystemuser ds = this.getDdSystemuserDAO().findByLogid(
					request.getParameter("logid"));
			if (ds == null) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			if (!ds.getPassword().equals(request.getParameter("password"))) {
				sbf.append("<result>3</result></root>");
				out.print(sbf.toString());
				return null;
			}
			DdShop dshop = this.getDdShopDAO().findById(ds.getShopid());
			sbf.append("<result>1</result><userName>" + ds.getName()
					+ "</userName><shopid>" + ds.getShopid()
					+ "</shopid><type>" + dshop.getType() + "</type></root>");
			session.setAttribute("shoplogid", ds.getShopid());
			// System.out.println(sbf.toString());
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward shopedit(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			if (session.getAttribute("shoplogid") == null) {
				sbf.append("<error>-1</error></root>");
				out.print(sbf.toString());
				return null;
			}
			Integer shoplogid = (Integer) session.getAttribute("shoplogid");
			DdSystemuser du = this.getDdSystemuserDAO().findByShopid(shoplogid);
			String passwordold = request.getParameter("oldpassword");
			String password = request.getParameter("newpassword");
			if (!du.getPassword().equals(passwordold)) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			du.setPassword(password);
			this.getDdSystemuserDAO().save(du);
			sbf.append("<result>1</result></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 系统后台登录
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward systemlogin(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			DdSystemuser ds = this.getDdSystemuserDAO().findByLogid(
					request.getParameter("logid"), "1,2,3,4,5,7,8,9,10");
			if (ds == null) {
				sbf.append("<result>2</result></root>");
				out.print(sbf.toString());
				return null;
			}
			if (!ds.getPassword().equals(request.getParameter("password"))) {
				sbf.append("<result>3</result></root>");
				out.print(sbf.toString());
				return null;
			}

			sbf.append("<result>1</result><userid>" + ds.getId()
					+ "</userid><userName>" + ds.getName()
					+ "</userName><type>" + ds.getType() + "</type></root>");
			session.setAttribute("systemlogid", ds.getId());
			// System.out.println(sbf.toString());
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	//
	/**
	 * 系统登录信息
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward getsystemlogininfo(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			Integer id = (Integer) session.getAttribute("systemlogid");
			if (id == null) {
				sbf.append("<result>-1</result></root>");
				out.print(sbf.toString());
				return null;
			}
			DdSystemuser ds = this.getDdSystemuserDAO().findById(id);
			if (ds == null) {
				sbf.append("<result>-1</result></root>");
				out.print(sbf.toString());
				return null;
			}
			sbf.append("<result>1</result><userid>" + ds.getId()
					+ "</userid><userName>" + ds.getName()
					+ "</userName><type>" + ds.getType() + "</type></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
}
